<script>
import http_config from "@/config/http";

export default {
  name: "uploadImage",
  props: {

  },
  data(){
    return {
      baseApiUrl: http_config.base_url,
      imgList : [],
      shortUrl:[],
    }
  },
  methods: {

    ChooseImage() {
      var that=this;
      let user = uni.getStorageSync("xykeep:user");
      let userToken = user.token;
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album','camera'],
        success: (res) => {
          const tempFilePaths = res.tempFilePaths;

          uni.uploadFile({
            url: that.baseApiUrl + '/addons/alioss/index/upload',
            filePath: tempFilePaths[0],
            fileType: 'image',
            name: 'file',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'multipart/form-data',
              'token': userToken
            },
            formData: {},
            success: (uploadFileRes) => {
              var dataimg=JSON.parse(uploadFileRes.data);
              if(!dataimg.data.url){
                this.$xyfun.msg('上传失败');
              }else {
                this.$emit("chooseFile", dataimg.data.url)
              }
            },
            fail: (error) => {
              if (error && error.response) {
                this.$xyfun.msg(error.response);
              }
            },
            complete: () => {
              setTimeout(function () {
                uni.hideLoading();
              }, 250);
            },
          });

        }
      });
    },

  }
}
</script>

<template>
  <view class="choose_image" @tap="ChooseImage"></view>
</template>

<style scoped lang="scss">
.choose_image{

}
</style>